"""
Created on 17-Aug-2009

@author: dexen
"""

InitType = "init_type"
TaskType = "task_type"


class InputUnit(object):
    def __init__(self, task_name, inds):
        self.task_name = task_name
        self.inds = inds
        self.transact_id = None
    
        

FAILED = "CRASHED"
SUCCEDED = "SUCCESS"

class OutputUnit(object):
    def __init__(self, slave_name, task_name, inds, exec_time, msg, in_size, transact_id):
        self.slave_name = slave_name
        self.task_name = task_name
        self.inds = inds
        self.msg = msg
        self.state = SUCCEDED
        self.exec_time = exec_time
        self.in_size = in_size
        self.transact_id = transact_id
                
    def crashed(self):
        self.state = FAILED

    def is_crashed(self):
        return self.state == FAILED

    def get_msg(self):
        return self.msg

    def get_exec_time(self):
        return self.exec_time
    
    def get_results(self):
        return self.inds
    
    def __repr__(self):
        msg = "Transaction id: " + str(self.transact_id) + " " + \
        self.slave_name + ", " + self.task_name + " processed " + \
        str(self.in_size) + " inds in " + \
        str(round(self.exec_time, 2)) + " secs\n" + self.msg + "\n"  
        return msg

class Transaction(object):
    def __init__(self, id, task_name, slave_name, input):
        self.id = id
        self.task_name = task_name
        self.slave_name = slave_name
        self.input = input
        self.output = None
        self.exec_time = None
        self.out_msg = None
        self.err_msg = None
        self.status = None

    def __repr__(self):
        msg = self.slave_name + ", " + self.task_name + " processed " + \
        str(self.in_size) + " individuals in " + \
        str(round(self.exec_time, 2)) + " secs\n" + self.msg + "\n"  
        return msg    
    

class Task(object):
    def __initialize__(self):
        pass

    def __isvalid__(self, ind):
        pass
    
    def __execute__(self, inds=[]):
        pass

class InitTask(object):
    def __initialize__(self, inds=[]):
        raise NotImplementedError()

def create_Task(name, classname, inputsize):
    task = TaskInfo()
    task.modulename = classname.__module__
    task.classname = classname.__name__
    #task.name = "%s.%s" %(task.modulename, task.classname)
    task.name = name
    task.type = TaskType
    task.input_size = inputsize
    return task

def create_InitTask(classname, outputsize):
    task = create_Task("Init", classname, 0)
    task.type = InitType
    return task

class TaskInfo(object):
    def __init__(self, name=None, modulename=None, classname=None, type=None):
        self.name = name
        self.modulename = modulename
        self.classname = classname
        self.type = type
        self.input_size = 0
        self.obj = None # referencing object of Task

    def _get_module(self, modulename):
        return __import__(modulename, fromlist=[""])
    
    def _get_cls(self, modulename, classname):
        module = self._get_module(modulename)
        return getattr(module, classname)
    
    def get_obj(self):
        return self._get_cls(self.modulename, self.classname)()
    
    def __repr__(self):
        return "%s %s %s %s %d" %(self.name, self.modulename, self.classname, 
                                  self.type, self.input_size)

